package com.ede.roverhu.simplebackstage.Http;


import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.ede.roverhu.simplebackstage.Activities.LoginActivity;
import com.ede.roverhu.simplebackstage.Constant.HandlerConstant;
import com.ede.roverhu.simplebackstage.Domain.QueryClassDomain;
import com.ede.roverhu.simplebackstage.Domain.QueryDomain;
import com.ede.roverhu.simplebackstage.Domain.VcodeMessageDomain;
import com.ede.roverhu.simplebackstage.Domain.xueyuanDomain;
import com.ede.roverhu.simplebackstage.Utils.GsonUtils;
import com.ede.roverhu.simplebackstage.Utils.ImgUtils;
import com.ede.roverhu.simplebackstage.Utils.LogUtils;
import com.ede.roverhu.simplebackstage.Utils.StringConv;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;


/**
 * Created by Admin on 2017/6/20.
 */

public class HttpClient {


    private static AsyncHttpClient mAsyncHttpClient = new AsyncHttpClient();

    //登录
    public static void logIn(final Handler handler, String username, String password, String vcode, final Context context) {
        RequestParams params = new RequestParams();
        params.put("username", username);
        params.put("password", password);
        params.put("vcode", vcode);
        mAsyncHttpClient.post("http://wise.xcollege.cn/ms_admin/system/ms_verifylogin.php", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                try {
                    String s = new String(bytes, "UTF-8");
                    if (s.contains("登录成功")) {
                        //LogUtils.e(myCookieStore.toString());
                        handler.sendEmptyMessage(HandlerConstant.LOGIN_SUCCESS);
                    } else {
                        Message msg = Message.obtain();
                        msg.what = HandlerConstant.LOGIN_ERROR;
                        msg.obj = s;
                        handler.sendMessage(msg);
                    }

                } catch (Exception e) {
                    handler.sendEmptyMessage(HandlerConstant.LOGIN_FAIL);
                    e.printStackTrace();

                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                handler.sendEmptyMessage(HandlerConstant.LOGIN_FAIL);
            }
        });

    }

    //获取验证码
    public static void getVcodeImg(final Handler handler, final Context context) {
        PersistentCookieStore myCookieStore = new PersistentCookieStore(context);
        mAsyncHttpClient.setCookieStore(myCookieStore);
        mAsyncHttpClient.get("http://wise.xcollege.cn/ms_app/php/imgval/", new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, org.apache.http.Header[] headers, byte[] bytes) {

                VcodeMessageDomain vcodeMessageDomain = new VcodeMessageDomain();
                vcodeMessageDomain.drawable = ImgUtils.Bytes2Drawable(bytes);

                Message msg = Message.obtain();
                msg.what = HandlerConstant.VCODE_SUCCESS;
                msg.obj = vcodeMessageDomain;
                handler.sendMessage(msg);
            }

            @Override
            public void onFailure(int i, org.apache.http.Header[] headers, byte[] bytes, Throwable throwable) {
                handler.sendEmptyMessage(HandlerConstant.VCODE_FAIL);
            }
        });
    }

    //人员查询
    public static void queryMember(QueryDomain queryDomain, final int page, final Handler handler) {
        String str = "";
        if (queryDomain.edit != null)
            str = StringConv.String2URLcode(queryDomain.edit.getText().toString());
        String url = "http://wise.xcollege.cn/ms_admin/app/member.php?act=list&classid=10&a=search&sid=" + queryDomain.sid + "&fid=" + queryDomain.fid + "&bid=" + queryDomain.bid + "&mid=" + queryDomain.mid + "&cid=" + queryDomain.cid + "&page=" + page + "&q=" + str;
        mAsyncHttpClient.get(url, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                String s = "";
                try {
                    s = new String(bytes, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                Message msg = Message.obtain();
                msg.obj = s;
                if (page == 1) {
                    msg.what = HandlerConstant.QUERYMEMBER_SUCCESS4P1;
                } else {
                    msg.what = HandlerConstant.QUERYMEMBER_SUCCESS4PM;
                }
                handler.sendMessage(msg);
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                handler.sendEmptyMessage(HandlerConstant.QUERYMEMBER_FAIL);
            }
        });

    }

    //学院查询
    public static void queryfid(QueryDomain queryDomain, final Handler handler) {
        String url = "http://wise.xcollege.cn/ms_admin/json/ms_framework.php?act=getFid&sid=" + queryDomain.sid;
        mAsyncHttpClient.get(url, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                try {
                    String json = new String(bytes, "UTF-8");
                    ArrayList<xueyuanDomain> xylist = GsonUtils.jsonToList(json, xueyuanDomain.class);
                    Message msg = Message.obtain();
                    msg.obj = xylist;
                    msg.what = HandlerConstant.QUERYFID_SUCCESS;
                    handler.sendMessage(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                handler.sendEmptyMessage(HandlerConstant.QUERYFID_FAIL);
            }
        });

    }

    //系查询
    public static void querybid(QueryDomain queryDomain, final Handler handler) {
        String url = "http://wise.xcollege.cn/ms_admin/json/ms_framework.php?act=getBid&pid=" + queryDomain.fid + "&sid=" + queryDomain.sid;
        mAsyncHttpClient.get(url, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                try {
                    String json = new String(bytes, "UTF-8");
                    ArrayList<xueyuanDomain> xilist = GsonUtils.jsonToList(json, xueyuanDomain.class);
                    Message msg = Message.obtain();
                    msg.obj = xilist;
                    msg.what = HandlerConstant.QUERYBID_SUCCESS;
                    handler.sendMessage(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                handler.sendEmptyMessage(HandlerConstant.QUERYBID_FAIL);
            }
        });

    }

    //专业查询
    public static void querymid(QueryDomain queryDomain, final Handler handler) {
        String url = "http://wise.xcollege.cn/ms_admin/json/ms_framework.php?act=getMid&pid=" + queryDomain.bid + "&sid=" + queryDomain.sid;
        mAsyncHttpClient.get(url, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                try {
                    String json = new String(bytes, "UTF-8");
                    ArrayList<xueyuanDomain> list = GsonUtils.jsonToList(json, xueyuanDomain.class);
                    Message msg = Message.obtain();
                    msg.obj = list;
                    msg.what = HandlerConstant.QUERYMID_SUCCESS;
                    handler.sendMessage(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                handler.sendEmptyMessage(HandlerConstant.QUERYMID_FAIL);
            }
        });

    }

    //班级查询
    public static void querycid(QueryDomain queryDomain, final Handler handler) {
        String url = "http://wise.xcollege.cn/ms_admin/json/ms_framework.php?act=getCid&pid=" + queryDomain.mid + "&sid=" + queryDomain.sid;
        mAsyncHttpClient.get(url, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                try {
                    String json = new String(bytes, "UTF-8");
                    ArrayList<xueyuanDomain> list = GsonUtils.jsonToList(json, xueyuanDomain.class);
                    Message msg = Message.obtain();
                    msg.obj = list;
                    msg.what = HandlerConstant.QUERYCID_SUCCESS;
                    handler.sendMessage(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                handler.sendEmptyMessage(HandlerConstant.QUERYCID_FAIL);
            }
        });

    }

    //http://wise.xcollege.cn/ms_admin/app/member.php?act=edit&id=36526&classid=10&a=edit
    //http://wise.xcollege.cn/ms_admin/app/member.php?act=edit&classid=10&id=36526
    //人员详情
    public static void queryMan(String id, final Handler handler) {
        LogUtils.e("queryMan");
        String url = "http://wise.xcollege.cn/ms_admin/app/member.php?act=edit&id=" + id + "&classid=10&a=edit";
        mAsyncHttpClient.get(url, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                try {
                    String info = new String(bytes, "UTF-8");
                    Message msg = Message.obtain();
                    msg.obj = info;
                    msg.what = HandlerConstant.QUERYMAN_SUCCESS;
                    handler.sendMessage(msg);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();

                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                handler.sendEmptyMessage(HandlerConstant.QUERYMAN_FAIL);
            }
        });

    }

    //教室详情
    public static void queryClass(String id, final Handler handler) {
        String url = "http://wise.xcollege.cn/ms_admin/app/study.php?act=edit&id=" + id + "&classid=116";
        mAsyncHttpClient.get(url, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                try {
                    String info = new String(bytes, "UTF-8");
                    Message msg = Message.obtain();
                    msg.obj = info;
                    msg.what = HandlerConstant.QUERYCLASS_SUCCESS;
                    handler.sendMessage(msg);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();

                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                handler.sendEmptyMessage(HandlerConstant.QUERYCLASS_FAIL);
            }
        });

    }

    //课程详情
    public static void queryCourse(String id, final Handler handler) {
        //http://wise.xcollege.cn/ms_admin/app/course.php?act=edit&id=1018217&classid=114
        String url = "http://wise.xcollege.cn/ms_admin/app/course.php?act=edit&id=" + id + "&classid=114";
        mAsyncHttpClient.get(url, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                try {
                    String info = new String(bytes, "UTF-8");
                    Message msg = Message.obtain();
                    msg.obj = info;
                    msg.what = HandlerConstant.QUERYCLASSTIMETABLEINFO_SUCCESS;
                    handler.sendMessage(msg);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();

                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                handler.sendEmptyMessage(HandlerConstant.QUERYCLASSTIMETABLEINFO_FAIL);
            }
        });

    }

    //课表查询
    public static void queryTimetable(final int page, final Handler handler) {
        String url = "http://wise.xcollege.cn/ms_admin/app/course.php?classid=114&act=list&page=" + page;
        mAsyncHttpClient.get(url, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                if (page == 1) {
                    try {
                        String info = new String(bytes, "UTF-8");
                        Message msg = Message.obtain();
                        msg.obj = info;
                        msg.what = HandlerConstant.QUERYTIMETABLE_SUCCESS;
                        handler.sendMessage(msg);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                } else {
                    try {
                        String info = new String(bytes, "UTF-8");
                        Message msg = Message.obtain();
                        msg.obj = info;
                        msg.what = HandlerConstant.QUERYTIMETABLE_SUCCESS4PM;
                        handler.sendMessage(msg);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                handler.sendEmptyMessage(HandlerConstant.QUERYTIMETABLE_FAIL);
            }
        });
    }

    //教室管理
    //http://wise.xcollege.cn/ms_admin/app/study.php?classid=116&act=list
    public static void querySchoolClass(QueryDomain queryDomain, final int page, final Handler handler) {
        String url = "";
        if (queryDomain.edit == null) {
            url = "http://wise.xcollege.cn/ms_admin/app/study.php?classid=116&act=list&page=" + page;
        } else {
            url = "http://wise.xcollege.cn/ms_admin/app/study.php?classid=116&a=search&act=list&q=" + StringConv.String2URLcode(queryDomain.edit.getText().toString()) + "&page=" + page;
        }
        LogUtils.e(url);
        mAsyncHttpClient.get(url, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {

                if (page == 1) {
                    try {
                        String info = new String(bytes, "UTF-8");
                        Message msg = Message.obtain();
                        msg.obj = info;
                        msg.what = HandlerConstant.QUERYSCHOOLCLASS_SUCCESS;
                        handler.sendMessage(msg);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                } else {
                    try {

                        String info = new String(bytes, "UTF-8");
                        Message msg = Message.obtain();
                        msg.obj = info;
                        msg.what = HandlerConstant.QUERYSCHOOLCLASS_SUCCESS4PM;
                        handler.sendMessage(msg);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                handler.sendEmptyMessage(HandlerConstant.QUERYSCHOOLCLASS_FAIL);
            }
        });
    }


    /**
     * 课表查询
     *
     * @return
     */
    public static void queryTimetable4Class(QueryClassDomain queryClassDomain, final int page, final Handler handler) {
        //http://wise.xcollege.cn/ms_admin/app/course.php?id=75&act=listc&classid=114&a=search&all=0&q=2017-06-28&cid=3925

        String url = "http://wise.xcollege.cn/ms_admin/app/course.php?act=listc&a=search&all=0&id=" + queryClassDomain.id + "&classid=114&page=" + page + "&q=" + queryClassDomain.time + "&cid=" + queryClassDomain.cid;
        LogUtils.e(url);
        mAsyncHttpClient.get(url, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                if (page == 1) {
                    try {
                        String info = new String(bytes, "UTF-8");
                        Message msg = Message.obtain();
                        msg.obj = info;
                        msg.what = HandlerConstant.QUERYCLASSTIMETABLE_SUCCESS;
                        handler.sendMessage(msg);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                } else {
                    try {

                        String info = new String(bytes, "UTF-8");
                        Message msg = Message.obtain();
                        msg.obj = info;
                        msg.what = HandlerConstant.QUERYCLASSTIMETABLE_SUCCESS4PM;
                        handler.sendMessage(msg);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                handler.sendEmptyMessage(HandlerConstant.QUERYCLASSTIMETABLE_FAIL);
            }
        });
    }

    public static AsyncHttpClient getAsyncHttpClient() {
        return mAsyncHttpClient;
    }
}
